package com.palmshop.online.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.palmshop.online.dao.CusDao;
import com.palmshop.online.entity.CusAddr;
import com.palmshop.online.entity.Customer;
import com.palmshop.online.entity.PalmResult;
import com.palmshop.online.service.CusService;
import com.palmshop.util.EmailUtils;
import com.palmshop.util.MapUtil;


/**
 * 用户相关的service实现类
 * @author quan
 *
 */
@Service
@Transactional
public class CusServiceImpl implements CusService {
	
	@Resource
	private CusDao dao;


	/**
	 * 用户注册
	 * @param cus:用户实体类对象
	 * @return result
	 */
	@Override
	public PalmResult addCus(Customer cus) {

		PalmResult result=new PalmResult();
		if(dao.addCus(cus)>0){
			result.setStatus(1);
		}
		
		return result;
	}

	/**
	 * 注册验证 当前用户是否已注册
	 * @param cusAccount:用户名
	 * @return result
	 */
	@Override
	public PalmResult findCusExist(String cusAccount) {
		
		PalmResult result=new PalmResult();
		List<Map<String,Object>>list=dao.findCusExist(cusAccount);
		if(list!=null&&list.size()>0){
			result.setStatus(1);
		}
		return result;
	}

	
	/**
	 * 登录验证 当前用户
	 * @param map
	 * @return result
	 */
	@Override
	public PalmResult findCheckLogin(HttpServletRequest request,Map<String, Object> map) {
		PalmResult result=new PalmResult();
		String cusCode=(String)request.getSession().getAttribute("code");
		
		List<Map<String,Object>>list=dao.findCheckLogin(map);
		if(list!=null&&list.size()>0){
			result.setStatus(1);
			result.setData(list.get(0));
		}
				
		
		
//		if(cusCode!=null&&cusCode.equals(map.get("cusCode"))||"app".equals(map.get("cusCode"))){
//				
//			List<Map<String,Object>>list=dao.findCheckLogin(map);
//					
//			if(list!=null&&list.size()>0){
//				result.setStatus(1);
//				result.setData(list.get(0));
//			}
//		}else{
//			result.setMsg("验证码不正确");
//		}
		
		
		return result;
	}


	/**
	 * 根据用户ip获取用户的当前位置:保留
	 * @param request
	 * @return result
	 */
	public PalmResult getLocation(HttpServletRequest request){
		PalmResult result=new PalmResult();
		
//		String localIp=Qutil.getIp(request);
		
		String localIp=MapUtil.getIpAddress(request);//获取用户的ip
		
		
		if("127.0.0.1".equals(localIp)){
			
			localIp="112.233.7.171";
		}
		
		String[]locationLt=MapUtil.getIPXY(localIp);
		System.out.println("lookme!!!!!!!!"+localIp+";"+locationLt);
		
		if(locationLt!=null){
			result.setStatus(1);
			result.setData(locationLt);
		}
		return result;
		
	}
	
	
	/**
	 * 查询用户的基本信息
	 * @param cusUnique:用户唯一性标识
	 * @return result
	 */
	public PalmResult findCusById(String cusUnique){
		PalmResult result=new PalmResult();
		List<Map<String,Object>>list=dao.findCusById(cusUnique);
		if(list!=null&&list.size()>0){
			result.setStatus(1);
			System.out.println(list);
			result.setData(list.get(0));
		}
		return result;
	}
	
	/**
	 * 根据用户的id修改对应的用户基本信息
	 * @param cus:用户实体类对象
	 * @return result
	 */
	public PalmResult updateCusById(Customer cus){
		PalmResult result=new PalmResult();
		if(dao.updateCusById(cus)>0){
			result.setStatus(1);
		}
		return result;
	}

	
	/**
	 * 根据用户id修改对应的用户密码
	 * @param map
	 * @return result
	 */
	public PalmResult updatePassWord(Map map){
		PalmResult result=new PalmResult();
		if(dao.updatePassWord(map)>0){
			result.setStatus(1);
		}
		return result;
	}
	
	
	
	/**
	 * 根据用户id确认密码是否正确
	 * @param map
	 * @return result
	 */
	public PalmResult findRWByCusId(Map map){
		PalmResult result=new PalmResult();
		List<Map<String,Object>>list=dao.findRWByCusId(map);
		if(list!=null&&list.size()>0){
			result.setStatus(1);
		}
		return result;
	}
	
	
	
	/**
	 * 根据用户的邮箱找回密码
	 * @param cusEmail:用户邮箱
	 * @return result
	 */
	public PalmResult findPwd(String cusEmail){
		PalmResult result=new PalmResult();
		List<Map<String,Object>>list=dao.findPwd(cusEmail);
		if(list!=null&&list.size()>0){
			String cusPwd=(String)list.get(0).get("cusPwd");
			try {
				EmailUtils.sendResetPasswordEmail(cusPwd,cusEmail);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			result.setStatus(1);
		}
		return result;
	}
	
	
	/* >>>>>>>>>>>>>用户积分>>>>>>>>>>>>>>>>>>>>>>>>>> */
	/**
	 * 根据用户的id查询对应的积分信息
	 * @param cusUnique:用户唯一性标识
	 * @return result
	 */
	public  PalmResult findPointsById(String cusUnique){
		PalmResult result=new PalmResult();
		List<Map<String,Object>>list=dao.findPointsById(cusUnique);
		
		if(list!=null&&list.size()>0){
			result.setStatus(1);
			result.setData(list);
		}
		return result;
	}
	
	/**
	 * 根据用户唯一性标识和店铺唯一性标识查询 该用户对应该店铺是否有积分信息
	 * @param map
	 * @return result
	 */
	public PalmResult findPointsExistById(Map map){
		PalmResult result=new PalmResult();
		List<Map<String,Object>>list=dao.findPointsExistById(map);
		if(list!=null&&list.size()>0){
			result.setStatus(1);
			result.setData(list.get(0).get("cus_points_id"));
			result.setTotals((Integer)list.get(0).get("cus_shop_points"));
			dao.updatePointsById(map);
		}
		return result;
		
	}

	/**
	 * 向收货地址表中插入一条新的数据
	 * @param cusAddr:用户地址实体类对象
	 * @return result
	 */
	public PalmResult addCusAddr(CusAddr cusAddr) {
		PalmResult result = new PalmResult();
		int addrId = dao.addAddr(cusAddr);
		if (addrId > 0) {
			result.setStatus(1);
			result.setData(addrId);
		}
		return result;
	}
	
	
	/**
	 * 根据地址id删除对应的地址信息
	 * @param addrId：地址id
	 * @return result
	 */
	public PalmResult deleteAddr(Integer addrId){
		PalmResult result=new PalmResult();
		if(dao.deleteAddr(addrId)>0){
			result.setStatus(1);
		}
		return result;
	}
	
	

	/**
	 * 根据地址id修改对应的地址信息
	 * 
	 * @param map
	 * @return result
	 */
	public PalmResult updateAddrById(Map map) {
		PalmResult result = new PalmResult();
		List<Map<String,Object>>list=dao.findAddrById((String)map.get("cusUnique"));
		if(list!=null&&list.size()>0){
			if (dao.updateAddrById(map) > 0) {
				result.setStatus(1);
			}
		}else{
			CusAddr cusAddr=new CusAddr();
			cusAddr.setAddrAddr((String)map.get("addrAddr"));
			cusAddr.setAddrPhone((String)map.get("addrPhone"));
			cusAddr.setAddrReceiverName((String)map.get("addrReceiverName"));
			cusAddr.setCusUnique((String)map.get("cusUnique"));
			
			if(dao.addAddr(cusAddr)>0){
				result.setStatus(1);
			}
		}
		
		return result;
	}
	
	
	/**
	 * 个人中心根据地址id修改对应的地址信息
	 * 
	 * @param map
	 * @return result
	 */
	public PalmResult updateAddrByIdP(Map map) {
		PalmResult result = new PalmResult();
		Integer addrId = (Integer) map.get("addrId");
		if (addrId > 0) {
			if (dao.updateAddrById(map) > 0) {
				result.setStatus(1);
			}
		} else {
			
			CusAddr cusAddr = new CusAddr();
			cusAddr.setAddrAddr((String) map.get("addrAddr"));
			cusAddr.setAddrPhone((String) map.get("addrPhone"));
			cusAddr.setAddrReceiverName((String) map.get("addrReceiverName"));
			cusAddr.setCusUnique((String) map.get("cusUnique"));
			cusAddr.setAddrDefault(2);

			if (dao.addAddr(cusAddr) > 0) {
				result.setStatus(1);
			}
		}
		
		return result;
	}
	

	/**
	 * 修改用的默认地址
	 * @param cusUnique:用户唯一性标识
	 * @param addrId：地址id
	 * @return result
	 */
	public PalmResult updateAddrDefByCus(String cusUnique,Integer addrId){
		PalmResult result=new PalmResult();
		if(dao.updateAddrDefByCus(cusUnique)>0&&dao.updateAddrDefById(addrId)>0){
			result.setStatus(1);
		}
		return result;
	}
	
	

	/**
	 * 根据地址id查询对应的地址信息
	 * @param addrId:地址id
	 * @return result
	 */
	public PalmResult findAddrByIdA(Integer addrId){
		PalmResult result = new PalmResult();
		List<Map<String, Object>> list = dao.findAddrByIdA(addrId);
		if (list != null && list.size() > 0) {
			result.setStatus(1);
			result.setData(list.get(0));
		}
		return result;
	}
	
	

	/**
	 * 根据用户的id查询对应的地址信息
	 * @param cusUnique:用户唯一性标识
	 * @return result
	 */
	public PalmResult findAddrByCusId(String cusUnique) {
		PalmResult result = new PalmResult();
		List<Map<String, Object>> list = dao.findAddrByCusId(cusUnique);
		
		if (list != null && list.size() > 0) {
			result.setStatus(1);
			result.setData(list);
		}
		return result;

	}
	
	
	/**
	 * 根据用户的id查询对应的地址信息
	 * @param cusUnique:用户唯一性标识
	 * @return result
	 */
	public PalmResult findAddrById(String cusUnique) {
		PalmResult result = new PalmResult();
		List<Map<String, Object>> list = dao.findAddrById(cusUnique);
		
		if (list != null && list.size() > 0) {
			result.setStatus(1);
			result.setData(list);
		}
		return result;

	}

	/* >>>>>end 用户地址 end>>>>>>>>>>>>>>>>>>> */
	
	

}
